module Admin::Resources::DataTypes::HasAndBelongsToManyHelper

  def display_has_and_belongs_to_many(item, attribute)
    item.send(attribute).map(&:to_label).join(', ')
  end

  def table_has_and_belongs_to_many_field(attribute, item)
    item.send(attribute).map(&:to_label).join(', ')
  end

  alias_method :table_has_many_field, :table_has_and_belongs_to_many_field

  def typus_has_and_belongs_to_many_field(attribute, form)
    klass = @resource.reflect_on_association(attribute.to_sym).class_name.constantize

    resource_ids = "#{attribute.singularize}_ids"
    html_options = {}
    model = @resource.name.underscore.gsub('/', '_')
    options = { attribute: "#{model}_#{attribute}" }

    label_text = @resource.human_attribute_name(attribute)
    if (text = build_label_text_for_has_and_belongs_to_many(klass, html_options, options))
      label_text += " <small>#{text}</small>"
    end

    locals = {
      attribute: attribute,
      attribute_id: "#{model}_#{attribute}",
      related_klass: klass,
      related_items: @item.send(attribute),
      related_ids: "#{model}[#{resource_ids}][]",
      values: klass,
      form: form,
      label_text: label_text.html_safe,
      html_options: html_options,
    }

    render 'admin/templates/has_and_belongs_to_many', locals
  end

  def build_label_text_for_has_and_belongs_to_many(klass, html_options, options = {})
    if html_options[:disabled] == true
      I18n.t('typus.labels.read_only')
    elsif admin_user.can?('create', klass) && !headless_mode?
      build_add_new_for_has_and_belongs_to_many(klass, options)
    end
  end

  def build_add_new_for_has_and_belongs_to_many(klass, options)
    html_options = set_modal_options_for(klass)
    html_options['url'] = url_for(controller: "/admin/#{klass.to_resource}", action: :new, _popup: true)
    html_options['data-controls-modal'] = "modal-from-dom-#{options[:attribute]}"

    options = { anchor: html_options['data-controls-modal'] }

    link_to I18n.t('typus.buttons.add'), options, html_options
  end

end
